import $ajax from '../../utils/request'
import common from '../../utils/common'

const user = {
    state: {
        user: '' // 用于保存用户信息
    },

    mutations: {
        SET_USER: (state, user) => {
            let info = state.user
            state.user = {...info, ...user}

        },
        SET_LOGOUT: (state, user) => {
            state.user = ''
        }
    },

    actions: {
        setUser({commit}, user) {
            commit('SET_USER', user)
        },
        loginBySession({commit, state, dispatch}) {
            // 请求登录信息，成功后赋值给user对象
            return new Promise((resolve, reject) => {
                let token = common.getUrlParam('token_enc'),
                    params = token ? {token_enc: token} : {}
                params.noAlert = true
                // $ajax.get('/login/loginTest?userId=1', params).then(res => {
                    $ajax.get('/index/webUserInfo', params).then(res => {
                    if (res.data) {
                        dispatch('setUser', res.data)
                    }
                    resolve(res.data)
                }).catch(err => {
                    reject(err)
                })
            })
        }
    }
}

export default user
